package com.tj.collection.db.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONArray;
import com.fqgj.base.services.redis.RedisClient;
import com.fqgj.common.api.Response;
import com.fqgj.common.utils.StringUtils;
import com.fqgj.msg.utils.Paged;
import com.kly.user.dto.SwitchManageDto;
import com.kly.user.dto.SwitchManagePageDto;
import com.kly.user.dto.SwitchManageQueryDto;
import com.kly.user.service.UserSwitchManageService;
import com.tj.collection.bean.RedisKey;
import com.tj.collection.controller.switchs.request.SwitchManageAddOrUpdateRequest;
import com.tj.collection.controller.switchs.request.SwitchManageQueryRequest;
import com.tj.collection.controller.switchs.response.SwitchManagerResponse;
import com.tj.collection.db.entity.SwitchManageEntity;
import com.tj.collection.db.entity.SwitchPackageNameManageEntity;
import com.tj.collection.db.enums.SwitchTypeEnum;
import com.tj.collection.db.po.SwitchManageQueryPo;
import com.tj.collection.db.service.SwitchManageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName: SwitchManageServiceImpl
 * @description: 开关管理service实现
 * @author: yuwn
 * @create: 2023-11-23 15:13
 */
@Service("switchManageService")
@Slf4j
public class SwitchManageServiceImpl implements SwitchManageService {



    @Autowired
    UserSwitchManageService userSwitchManageService;

    @Override
    public Paged<List<SwitchManagerResponse>> getList(SwitchManageQueryRequest queryDTO) {

        SwitchManageQueryDto switchManageQueryDto = new SwitchManageQueryDto();
        BeanUtil.copyProperties(queryDTO,switchManageQueryDto);

        Response<SwitchManagePageDto> response = userSwitchManageService.getList(switchManageQueryDto);


        if(response.isSuccess()){
            if(response.getData() != null && response.getData().getSwitchManageDtos() != null){
                List<SwitchManagerResponse> switchManagerResponseDtos = response.getData().getSwitchManageDtos().parallelStream().map(x->{
                    SwitchManagerResponse switchManagerResponseDto = new SwitchManagerResponse();
                    BeanUtil.copyProperties(x,switchManagerResponseDto);
                    return switchManagerResponseDto;
                }).collect(Collectors.toList());
                //获取总数
                Integer totalCount = response.getData().getTotalCount();

                return new Paged<>(totalCount,queryDTO.getPageSize(),queryDTO.getCurrentPage(),switchManagerResponseDtos);
            }else{
                return new Paged<>(0,queryDTO.getPageSize(),queryDTO.getCurrentPage(),new ArrayList<>());
            }


        }else{
            return new Paged<>(0,queryDTO.getPageSize(),queryDTO.getCurrentPage(),new ArrayList<>());
        }


    }

    @Override
    public Boolean addOrUpdate(SwitchManageAddOrUpdateRequest addOrUpdateDto) {
        SwitchManageDto switchManageDto = new SwitchManageDto();
        BeanUtil.copyProperties(addOrUpdateDto,switchManageDto);

        return userSwitchManageService.addOrUpdate(switchManageDto).getData();
    }


    @Override
    public Boolean deleteById(Long id) {
        return userSwitchManageService.deleteById(id).getData();
    }

    @Override
    public List<String> getPackageList(Integer switchType) {
        return userSwitchManageService.getPackageList(switchType).getData();
    }
}
